package com.medicine.common.utils;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class BeanUtil {

	/**
	 * 复制，将srouces集合中的数据复制到另一个集合中
	 * 
	 * @param sources
	 * @param clazz
	 * @return
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * @throws Exception
	 */
	public static <T, S> List<T> copyPropertiesList(List<S> sources, Class<T> clazz)
			throws InstantiationException, IllegalAccessException {
		if (sources == null || clazz == null)
			throw new RuntimeException("null point exceptione");
		List<T> targets = new ArrayList<T>();
		for (S source : sources) {
			T target = clazz.newInstance();
			copyProperties(source, target);
			targets.add(target);
		}
		return targets;
	}

	/**
	 * 复制
	 * 
	 * @param source
	 * @param target
	 * @throws Exception
	 */
	public static <S, T> void copyProperties(S source, T target) {
		if (source == null || target == null)
			throw new RuntimeException("null point exceptione");
		BeanUtils.copyProperties(source, target);
	}
}
